/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-07-31
 * Time: 22:27
 */
class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
    public void print(){
        System.out.print(val+" ");
        if(left!=null){
            left.print();
        }
        if(right!=null){
            right.print();
        }
    }
}
public class Fun {
    public TreeNode p=null;
    public TreeNode Convert(TreeNode root) {
        if(root==null){
            return root;
        }
        TreeNode left;
        TreeNode r=root;
        while(r.left!=null){
            r=r.left;
        }
        left=r;
        fun(root);
        return left;
    }
    public void fun(TreeNode root){
        if(root==null){
            return ;
        }
        fun(root.left);
        root.left=p;
        if(p!=null) {
            p.right = root;
        }
        p=root;
        fun(root.right);
    }
}
